/**
 * 方法汇总
 * @returns 
 */

// 校验手机号格式
const testPhone = (phone) => {
	const phoneRegex = /^1[3-9]\d{9}$/;
	return phoneRegex.test(phone)
}

// 校验电子邮箱格式
const testEmail = (email) => {
	const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	return emailRegex.test(email)
}

// proxy对象转json
const toPlainObject = (obj) => {
	if (obj === null || typeof obj !== 'object') {
		return obj;
	}
	const result = {};
	for (const key in obj) {
		if (obj.hasOwnProperty(key)) {
			result[key] = toPlainObject(obj[key]);
		}
	}
	return result;
}

// 时间戳转时间
const timestampToDate = (timestamp) => {
  let dateObj = new Date(timestamp * 1000);
  let year = dateObj.getFullYear();
  let month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
  let day = ("0" + dateObj.getDate()).slice(-2);
  let hours = ("0" + dateObj.getHours()).slice(-2);
  let minutes = ("0" + dateObj.getMinutes()).slice(-2);
  let seconds = ("0" + dateObj.getSeconds()).slice(-2);
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 获取当前页面传来参数
const getOptions = () => {
	const pages = getCurrentPages(); // 获取当前页面栈
	const currentPage = pages[pages.length - 1]; // 获取当前页面实例
	return currentPage.options
}

// 提交报名信息校验
const checkFn = (type) => {
	switch(type) {
		case 'nationality':
			return "^[\\u4e00-\\u9fa5]{1,3}族?$";
			break;
		case 'idCard':
			return "^[1-9]\\d{5}(19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[\\dXx]$";
			break;
		case 'phone':
			return "^1[3-9]\\d{9}$";
			break;
	}
}


export {
	testPhone,
	testEmail,
	toPlainObject,
	timestampToDate,
	getOptions,
	checkFn,
	getImageUrl
}